﻿using AdminSystem.Domain;
using AdminSystem.EntityFrameworkCore;
using AdminSystem.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Quartz;

namespace AdminSystem.Application
{
    public class MyJob : IJob
    {
        private readonly ILogger<MyJob> logger;
        private readonly IServiceProvider serviceProvider;

        public MyJob(ILogger<MyJob> logger, IServiceProvider serviceProvider)
        {
            this.logger = logger;
            this.serviceProvider = serviceProvider;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            logger.LogInformation("xxxxxxxxxxx");
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetRequiredService<AdminSystemDbContext>();
                var book = await db.Addresses.FirstOrDefaultAsync();
                if (book != null)
                {
                    book.Province += "北京市";
                }
                await db.SaveChangesAsync();
            }            
        }
    }
}
